using System.Collections.Generic;
using UnityEngine;

public class Enemy : Creature
{
	[Header("Enemy - State")]
	public Vector2 direction;

	public IFood target;

	public string[] textAbilities;

	public string textPassiveAbility;

	public List<Hand> claws;

	public List<Hand> spikes;

	private int rayDistance = 4;

	private Transform healthBar;

	private SpriteRenderer spriteRenderer;

	private int layer = 32785;

	private const int constRayDistance = 4;

	public static GameObject healthBarObject;

	public void Begin(Vector3 position)
	{
		SetPosition(position);
		float num;
		if (Scene.current == SceneName.Game)
		{
			Vector3 vector = Library.player.GetPosition() - position;
			num = Mathf.Atan2(vector.y, vector.x) * 57.29578f + 90f;
		}
		else
		{
			num = Random.Range(0, 360);
		}
		SetRotation(num);
		Transform emptyTransform = Info.emptyTransform;
		emptyTransform.rotation = Quaternion.Euler(0f, 0f, num - 135f);
		float value = Random.value;
		direction = value * emptyTransform.right + (1f - value) * emptyTransform.up;
		int num2 = textAbilities.Length;
		Progress.Ability passiveAbility = new Progress.Ability(textPassiveAbility, 0);
		Progress.Ability[] array = new Progress.Ability[num2];
		for (int i = 0; i < num2; i++)
		{
			array[i] = new Progress.Ability(textAbilities[i], Info.enemyAbilityLevel);
		}
		CreatureLoader.claws = claws;
		CreatureLoader.spikes = spikes;
		CreatureLoader.creature = this;
		CreatureLoader.ApplyAbilities(array, passiveAbility);
		for (int j = 0; j < num2; j++)
		{
			abilities[j].AssignEnemyAbility(this);
		}
		speed *= 0.75f;
		StartLegsAnimation();
	}

	private void Update()
	{
		if (!Info.pause)
		{
			if (meatEater && target != null)
			{
				SetFollowingDirection();
			}
			if (CastRay(direction))
			{
				GetNewDirection();
			}
			Move(direction);
		}
	}

	public override void PlaySound()
	{
		if (meatEater)
		{
			Library.controller.PlayEnemySound(GetPosition());
		}
	}

	private bool CastRay(Vector2 direction)
	{
		return Physics2D.Raycast(rigidbody.position, direction, (float)rayDistance * transform.localScale.x, layer);
	}

	private void GetNewDirection()
	{
		Vector2[] array = new Vector2[3]
		{
			transform.right,
			-transform.right,
			transform.up
		};
		array.Shuffle();
		for (int i = 0; i < 3 && !IsDirectionAvailable(array[i]); i++)
		{
		}
	}

	private bool IsDirectionAvailable(Vector2 direction)
	{
		if (CastRay(direction))
		{
			return false;
		}
		this.direction = direction;
		EndEat();
		return true;
	}

	public void RunAway(IFood target)
	{
		direction = (rigidbody.position - target.GetPosition()).normalized;
	}

	public override void Eat(Food food, float size)
	{
		base.Eat(food, size);
		EndEat();
	}

	public void EndEat()
	{
		target = null;
		rayDistance = 4;
	}

	public void GoEat(IFood target)
	{
		if (meatEater)
		{
			this.target = target;
		}
		rayDistance = 2;
		SetFollowingDirection(target);
	}

	private void SetFollowingDirection()
	{
		SetFollowingDirection(target);
	}

	private void SetFollowingDirection(IFood target)
	{
		direction = (target.GetPosition() - rigidbody.position).normalized;
	}

	public override void SetHealth(float value)
	{
		base.SetHealth(value);
		if (health < maxHealth)
		{
			if (!healthBar)
			{
				CreateHealthBar();
			}
			UpdateHealthBar();
		}
		else if ((bool)healthBar)
		{
			Object.Destroy(healthBarParent.gameObject);
		}
	}

	private void CreateHealthBar()
	{
		healthBarParent = Object.Instantiate(healthBarObject, root).transform;
		healthBar = healthBarParent.GetChild(0);
		spriteRenderer = healthBar.GetComponent<SpriteRenderer>();
	}

	private void UpdateHealthBar()
	{
		float num = health / maxHealth;
		spriteRenderer.size = new Vector2(3.2f * num, spriteRenderer.size.y);
		healthBar.localPosition = new Vector3(-3.2f * (1f - num) * 0.5f, 0f);
	}

	public override void SetStamina(float value)
	{
	}

	public override void EnableSwimming(int level)
	{
		base.EnableSwimming(level);
		layer = 32769;
	}

	public override bool IsEnemy()
	{
		return true;
	}
}
